﻿using MySql.Data.MySqlClient;

namespace 发卡工具
{
    public partial class UnLockCodeList : Form
    {
        private const string Code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        private static readonly Random Random = new();
        public UnLockCodeList()
        {
            InitializeComponent();
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
            dataGridView1.GridColor = Color.Black;
        }
        
        private static string GenerateCode()
        {
            const int codeLength = 16;

            char[] code = new char[codeLength];
            for (int i = 0; i < codeLength; i++)
            {
                code[i] = Code[Random.Next(Code.Length)];
            }
            Console.WriteLine(new string(code));
            return new string(code);
        }
        
        private static void AddUnLockCode(string modName, string modUnzipPassword)
        {
            if (string.IsNullOrEmpty(modName) || string.IsNullOrEmpty(modUnzipPassword))
            {
                MessageBox.Show("请输入完整信息");
                return;
            }
            const string connect = "Server=101.132.40.214;Port=3306;Database=spirithun;Uid=spirithun;Pwd=euU9EgTWW8uOMt8m;";
            var conn = new MySqlConnection(connect);
    
            // 使用参数化查询来提高安全性
            const string sql = "INSERT INTO mod_list (mod_name, mod_unlock_code, mod_unzip_password, unlock_code_status) " +
                               "VALUES (@modName, @modUnlockCode, @modUnzipPassword, 0)";
    
            try
            {
                conn.Open();
                var cmd = new MySqlCommand(sql, conn);
        
                // 添加参数并设置它们的值
                cmd.Parameters.AddWithValue("@modName", modName);
                cmd.Parameters.AddWithValue("@modUnlockCode", GenerateCode()); // 假设GenerateCode返回字符串
                cmd.Parameters.AddWithValue("@modUnzipPassword", modUnzipPassword);

                int result = cmd.ExecuteNonQuery();
                Console.WriteLine(result > 0 ? "记录添加成功" : "没有记录被添加");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                MessageBox.Show($"发生错误: {e.Message}");
            }
            finally
            {
                conn.Close();
            }
        }


        private void GetUnLockCodeList()
        {
            if (dataGridView1.Rows.Count > 0) dataGridView1.Rows.Clear();
            const string connect = "Server=101.132.40.214;Port=3306;Database=spirithun;Uid=spirithun;Pwd=euU9EgTWW8uOMt8m;";
            var conn = new MySqlConnection(connect);
            try
            {
                conn.Open();
                Console.WriteLine("连接成功");
                const string sql = "select * from mod_list";
                var cmd = new MySqlCommand(sql, conn);
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    int index = dataGridView1.Rows.Add();
                    dataGridView1.Rows[index].Cells[0].Value = reader["mod_id"];
                    dataGridView1.Rows[index].Cells[1].Value = reader["mod_name"];
                    dataGridView1.Rows[index].Cells[2].Value = reader["mod_unlock_code"];
                    dataGridView1.Rows[index].Cells[3].Value = reader["mod_unzip_password"];
                    dataGridView1.Rows[index].Cells[4].Value = reader["unlock_code_status"];
                }
            }
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        private void UnLockCodeList_Load(object sender, EventArgs e)
        {
            GetUnLockCodeList();
        }

        private void Refresh_Click(object sender, EventArgs e)
        {
            GetUnLockCodeList();
        }

        private void Clone_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                int index = dataGridView1.SelectedRows[0].Index;
                string modName = dataGridView1.Rows[index].Cells[1].Value.ToString()?? "";
                string modUnzipPassword = dataGridView1.Rows[index].Cells[3].Value.ToString()??"";
                AddUnLockCode(modName, modUnzipPassword);
                GetUnLockCodeList();
            }
            else
            {
                MessageBox.Show("请选择要复制的Mod");
            }
        }

        private void Search_Click(object sender, EventArgs e)
        {
            dataGridView1.ClearSelection();
            if (!string.IsNullOrEmpty(SearchBox.Text))
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[1].Value == null || !row.Cells[1].Value.ToString()!.Equals(SearchBox.Text)) continue;
                    row.Selected = true;
                    break;
                }
            }

            if (dataGridView1.SelectedRows.Count > 0)
            {
                dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.SelectedRows[0].Index;
            }else
            {
                MessageBox.Show("未找到Mod");
            }
        }
    }
}